package com.ajie.bluetoothcard.db;


import java.util.ArrayList;

import com.ajie.bluetoothcard.domain.Card;
import com.ajie.bluetoothcard.domain.CardType;
import com.ajie.bluetoothcard.util.DateUtil;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;


/**
 * 数据库Api
 * 
 * @author BigMao
 */
public class DBApi {
	private static DBApi instance;
	private Context mContext;
	private Database mDb = null;

	public static DBApi getInstance(Context context) {
		if (instance == null) {
			instance = new DBApi(context);
		}
		return instance;
	}

	private DBApi(Context context) {
		mContext = context;
		openDatabase();
	}

	private void openDatabase() {
		try {
			if (mDb == null)
				mDb = new Database(mContext);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Database getDatabase() {
		return mDb;
	}
	
	
	

	public Card insertCard(Card card){
		ContentValues values = new ContentValues();
		values.put(Column.Card_name, card.getName());
		values.put(Column.Card_mobilePhone, card.getMobilePhone());
		values.put(Column.Card_telephone, card.getTelephone());
		values.put(Column.Card_avatar, card.getAvatar());
		values.put(Column.Card_type, card.getType());
		values.put(Column.Card_isDel, card.getIsDel());
		values.put(Column.Card_title, card.getTitle());
		values.put(Column.Card_company, card.getCompany());
		values.put(Column.Card_voide, card.getVoide());
		values.put(Column.Card_address, card.getAddress());
		values.put(Column.Card_introduction, card.getIntroduction());
		values.put(Column.Card_url, card.getUrl());
		values.put(Column.Card_email, card.getEmail());
		values.put(Column.Card_fax, card.getFax());
		values.put(Column.Card_time, DateUtil.createTime(System.currentTimeMillis()));
		int id = (int)mDb.insert(Table.Card, values);
		if(id == -1){
			return null;
		} else {
			card.setId(id+"");
			return card;
		}
	}
	
	public CardType insertCardType(CardType cardType){
		ContentValues values = new ContentValues();
		values.put(Column.Card_Type_ID, cardType.getType());
		values.put(Column.Card_Type_name, cardType.getName());
		values.put(Column.Card_Type_number, "0");
		int id = (int)mDb.insert(Table.Card_Type, values);
		if(id == -1){
			return null;
		} else {
			cardType.setId(id+"");
			return cardType;
		}
	}
	
	public void delCard(String cardId) {
		mDb.delete(Table.Card, Column.Card_ID + "=?" , new String[] {cardId});
	}
	
	public void updateRecord(Card card){
		ContentValues values = new ContentValues();
		values.put(Column.Card_name, card.getName());
		values.put(Column.Card_mobilePhone, card.getMobilePhone());
		values.put(Column.Card_telephone, card.getTelephone());
		values.put(Column.Card_avatar, card.getAvatar());
		values.put(Column.Card_type, card.getType());
		values.put(Column.Card_isDel, card.getIsDel());
		values.put(Column.Card_title, card.getTitle());
		values.put(Column.Card_company, card.getCompany());
		values.put(Column.Card_voide, card.getVoide());
		values.put(Column.Card_address, card.getAddress());
		values.put(Column.Card_introduction, card.getIntroduction());
		values.put(Column.Card_url, card.getUrl());
		values.put(Column.Card_email, card.getEmail());
		values.put(Column.Card_fax, card.getFax());
		values.put(Column.Card_time, card.getTime());
		mDb.update(Table.Card, values, Column.Card_ID + "=? ", new String[] { card.getId()});
	}

	public static final String SEARCHTYPE_ALL = "0";
	public static final String SEARCHTYPE_CONDITION = "1";
	public static final String SEARCHTYPE_ME = "2";
	
	/**
	 * 查询
	 * @param searchType 查询类型 0：所有 1：关键字查询 (如果为0，则 condition 可以为"")
	 * @param condition 关键字查询的名字
	 * @param cardTypes 类型
	 * @param isDelNum 是否被删除 0：否 1：是   (如果为1，则 cardTypes 可以为"")
	 * @return
	 */
	public ArrayList<Card> getAllCardsByType(String searchType,String condition,String cardTypes,String isDelNum){
		ArrayList<Card> cards = new ArrayList<Card>();
		Cursor cursor = null;
		if(SEARCHTYPE_ALL.equals(searchType)){
			if(SEARCHTYPE_ALL.equals(isDelNum)){
				if(SEARCHTYPE_ALL.equals(cardTypes)){
					cursor = mDb.query(Table.Card, null, Column.Card_isDel+"=? ", new String[] {"0"});
				}else{
					cursor = mDb.query(Table.Card, null, Column.Card_type+"=? and "+Column.Card_isDel+"=? ", new String[] {cardTypes,"0"});
				}
			}else{
				cursor = mDb.query(Table.Card, null, Column.Card_isDel+"=? ", new String[] {"1"});
			}
		}else{//查询关键字
			if(SEARCHTYPE_ALL.equals(isDelNum)){
				if(SEARCHTYPE_ALL.equals(cardTypes)){
					cursor = mDb.rawQuery("select * from "+Table.Card+" where "+Column.Card_name+" like '%"+condition+"%' and "+Column.Card_isDel+" =0 ;", null);
				}else{
					cursor = mDb.rawQuery("select * from "+Table.Card+" where "+Column.Card_name+" like '%"+condition+"%' and "+Column.Card_isDel+" =0 and "+Column.Card_type+" = "+cardTypes+";", null);
				}
			}else{//回收站中不需要查询关键字
//				cursor = mDb.query(Table.Card, null, Column.Card_isDel+"=? ", new String[] {"1"});
			}
		}
		int idIndex = cursor.getColumnIndex(Column.Card_ID);
		int nameIndex = cursor.getColumnIndex(Column.Card_name);
		int mobilePhoneIndex = cursor.getColumnIndex(Column.Card_mobilePhone);
		int telephoneIndex = cursor.getColumnIndex(Column.Card_telephone);
		int avatarIndex = cursor.getColumnIndex(Column.Card_avatar);
		int titleIndex = cursor.getColumnIndex(Column.Card_title);
		int companyIndex = cursor.getColumnIndex(Column.Card_company);
		int voideIndex = cursor.getColumnIndex(Column.Card_voide);
		int addressIndex = cursor.getColumnIndex(Column.Card_address);
		int introductionIndex = cursor.getColumnIndex(Column.Card_introduction);
		int timeIndex = cursor.getColumnIndex(Column.Card_time);
		int typeIndex = cursor.getColumnIndex(Column.Card_type);
		int isDelIndex = cursor.getColumnIndex(Column.Card_isDel);
		int urlIndex = cursor.getColumnIndex(Column.Card_url);
		int emailIndex = cursor.getColumnIndex(Column.Card_email);
		int faxIndex = cursor.getColumnIndex(Column.Card_fax);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(idIndex);
			String name = cursor.getString(nameIndex);
			String mobilePhone = cursor.getString(mobilePhoneIndex);
			String telephone = cursor.getString(telephoneIndex);
			String avatar = cursor.getString(avatarIndex);
			String title = cursor.getString(titleIndex);
			String company = cursor.getString(companyIndex);
			int type = cursor.getInt(typeIndex);
			int isDel = cursor.getInt(isDelIndex);
			String voide = cursor.getString(voideIndex);
			String address = cursor.getString(addressIndex);
			String introduction = cursor.getString(introductionIndex);
			String time = cursor.getString(timeIndex);
			String url = cursor.getString(urlIndex);
			String email = cursor.getString(emailIndex);
			String fax = cursor.getString(faxIndex);
			Card card = new Card();
			card.setId(id+"");
			card.setName(name);
			card.setMobilePhone(mobilePhone);
			card.setTelephone(telephone);
			card.setAvatar(avatar);
			card.setTitle(title);
			card.setCompany(company);
			card.setType(type+"");
			card.setIsDel(isDel+"");
			card.setVoide(voide);
			card.setAddress(address);
			card.setIntroduction(introduction);
			card.setTime(time);
			card.setUrl(url);
			card.setEmail(email);
			card.setFax(fax);
			cards.add(card);
		}
		cursor.close();
		return cards;
	}
	
	/** 获取个人信息 */
	public Card getMyCard(){
		Card card = null;
		Cursor cursor = mDb.query(Table.Card, null, Column.Card_isDel+"=? ", new String[] {"2"});
		int idIndex = cursor.getColumnIndex(Column.Card_ID);
		int nameIndex = cursor.getColumnIndex(Column.Card_name);
		int mobilePhoneIndex = cursor.getColumnIndex(Column.Card_mobilePhone);
		int telephoneIndex = cursor.getColumnIndex(Column.Card_telephone);
		int avatarIndex = cursor.getColumnIndex(Column.Card_avatar);
		int titleIndex = cursor.getColumnIndex(Column.Card_title);
		int companyIndex = cursor.getColumnIndex(Column.Card_company);
		int voideIndex = cursor.getColumnIndex(Column.Card_voide);
		int addressIndex = cursor.getColumnIndex(Column.Card_address);
		int introductionIndex = cursor.getColumnIndex(Column.Card_introduction);
		int timeIndex = cursor.getColumnIndex(Column.Card_time);
		int typeIndex = cursor.getColumnIndex(Column.Card_type);
		int isDelIndex = cursor.getColumnIndex(Column.Card_isDel);
		int urlIndex = cursor.getColumnIndex(Column.Card_url);
		int emailIndex = cursor.getColumnIndex(Column.Card_email);
		int faxIndex = cursor.getColumnIndex(Column.Card_fax);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(idIndex);
			String name = cursor.getString(nameIndex);
			String mobilePhone = cursor.getString(mobilePhoneIndex);
			String telephone = cursor.getString(telephoneIndex);
			String avatar = cursor.getString(avatarIndex);
			String title = cursor.getString(titleIndex);
			String company = cursor.getString(companyIndex);
			int type = cursor.getInt(typeIndex);
			int isDel = cursor.getInt(isDelIndex);
			String voide = cursor.getString(voideIndex);
			String address = cursor.getString(addressIndex);
			String introduction = cursor.getString(introductionIndex);
			String time = cursor.getString(timeIndex);
			String url = cursor.getString(urlIndex);
			String email = cursor.getString(emailIndex);
			String fax = cursor.getString(faxIndex);
			card = new Card();
			card.setId(id+"");
			card.setName(name);
			card.setMobilePhone(mobilePhone);
			card.setTelephone(telephone);
			card.setAvatar(avatar);
			card.setTitle(title);
			card.setCompany(company);
			card.setType(type+"");
			card.setIsDel(isDel+"");
			card.setVoide(voide);
			card.setAddress(address);
			card.setIntroduction(introduction);
			card.setTime(time);
			card.setUrl(url);
			card.setEmail(email);
			card.setFax(fax);
		}
		cursor.close();
		return card;
	}
	public ArrayList<CardType> getAllCardTypes(){
		ArrayList<CardType> cardTypes = new ArrayList<CardType>();
		Cursor cursor = mDb.query(Table.Card_Type, null, null, new String[] {});
		int idIndex = cursor.getColumnIndex(Column.Card_ID);
		int typeIdIndex = cursor.getColumnIndex(Column.Card_Type_ID);
		int nameIndex = cursor.getColumnIndex(Column.Card_Type_name);
		int numberIndex = cursor.getColumnIndex(Column.Card_Type_number);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(idIndex);
			String name = cursor.getString(nameIndex);
			String typeId = cursor.getString(typeIdIndex);
			int number = cursor.getInt(numberIndex);
			CardType cardType = new CardType();
			cardType.setId(id+"");
			cardType.setName(name);
			cardType.setType(typeId);
			cardType.setNumber(number+"");
			cardTypes.add(cardType);
		}
		cursor.close();
		return cardTypes;
	}
}
